package com.ccys.cms.controller.consultation;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.consultation.ConsultationEditDiagnosisService;
import com.ccys.cms.service.consultation.ConsultationEditReferralLetterService;
import com.ccys.cms.service.consultation.ConsultationEditSymptomService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.consultation.ConsultationEditDto;
import com.ccys.db.entity.*;
import com.ccys.db.mapper.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "问诊列表编辑接口( consultation List 的 edit )")
@RequestMapping("consultation")
public class ConsultationListEditController {



    @Resource
    private TblBookingMapper bookingMapper;

    @Resource
    private TblBookingSummaryMapper summaryMapper;

    @Resource
    private TblBookingFeeMapper feeMapper;

    @Resource
    private ConsultationEditSymptomService symptomService;

    @Resource
    private ConsultationEditDiagnosisService diagnosisService;

    @Resource
    private TblBookingConsultNoteMapper consultNoteMapper;

    @Resource
    private TblBookingPrescriptionMapper prescriptionMapper;

    @Resource
    private ConsultationEditReferralLetterService referralLetterService;


    @ApiOperation("编辑consultation")
    @PutMapping("editConsultation")
    @Transactional
    public ResponseResult editConsultation(ConsultationEditDto dto){
        edit(dto);
        return ResponseResult.success();
    }

    private void edit(ConsultationEditDto dto){
        TblBooking booking = new TblBooking();
        TblBookingSummary summary = new TblBookingSummary();
        TblBookingFee fee = new TblBookingFee();

        BeanUtils.copyProperties(dto,booking);
        BeanUtils.copyProperties(dto,summary);
        BeanUtils.copyProperties(dto,fee);
        String bookingId = dto.getBookingId();
        booking.setId(bookingId);
        summary.setId(bookingId);
        fee.setId(bookingId);

        bookingMapper.updateByPrimaryKey(booking);
        summaryMapper.updateByPrimaryKey(summary);
        feeMapper.updateByPrimaryKey(fee);

        List<TblBookingSymptom> symptomList = dto.getSymptomList();
        List<TblBookingDiagnosis> diagnosisList = dto.getDiagnosisList();
        List<TblBookingReferralLetter> letterList = dto.getLetterList();
        if(CollectionUtils.isNotEmpty(symptomList)){
            //删除以前的
            consultNoteMapper.deleteSymptoms(bookingId);
            symptomList.forEach(x->x.setId(IdUtil.getSnowflakeNextIdStr()));
            symptomService.insertAll(symptomList);
        }
        if(CollectionUtils.isNotEmpty(diagnosisList)){
            consultNoteMapper.deleteDiagnosis(bookingId);
            diagnosisList.forEach(x->x.setId(IdUtil.getSnowflakeNextIdStr()));
            diagnosisService.insertAll(diagnosisList);
        }

        if(CollectionUtils.isNotEmpty(letterList)){
            consultNoteMapper.deleteReferralLetter(bookingId);
            letterList.forEach(x->x.setId(IdUtil.getSnowflakeNextIdStr()));
            referralLetterService.insertAll(letterList);
        }
    }


    // TODO: 2024/1/4 0004 查询diagnosis、symptom集合
    // TODO: 2024/1/4 0004 查询关联的child ？
    // TODO: 2024/1/4 0004 查询consultation状态集合 ？
    // TODO: 2024/1/4 0004 科室医生的查询接口

    @ApiOperation("end consult")
    @PutMapping("endConsult")
    @Transactional
    public ResponseResult endConsult(ConsultationEditDto dto){
        edit(dto);
        //将status 改成 -1 subStatus 改成 -3
        TblBooking booking = new TblBooking();
        booking.setId(dto.getBookingId());
        booking.setStatus(-1);
        booking.setSubStatus(-3);
        int i = bookingMapper.updateByPrimaryKey(booking);
        return ResponseResult.update(i);
    }


    @ApiOperation("add to medication list")
    @PostMapping("addToMedicationList")
    @Transactional
    public ResponseResult addToMedicationList(TblBookingPrescription model){

        // TODO: 2024/1/4 0004 in_exclusion_list
        // TODO: 2024/1/4 0004 Purchased
        int i = prescriptionMapper.insert(model);
        return ResponseResult.update(i);
    }

    



}
